﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKSparseDistance.ModelLayer;
using YJKInspectModelFunc.ControlLayer;
using YJKInspectModelFunc.ModelLayer;

namespace YJKSparseDistance
{
	[Transaction(TransactionMode.Manual)]
	internal class TestCmd_RoomFindAndBoundaryCurveMake : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			Application application2 = application.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			List<Level> list = new List<Level>();
			List<Phase> list2 = new List<Phase>();
			list.Add(activeUIDocument.ActiveView.GenLevel);
			foreach (object obj in document.Phases)
			{
				Phase item = (Phase)obj;
				list2.Add(item);
			}
			List<RoomPacker_EvacuationDistance> roomPacker = BaseRoomFinder<RoomPacker_EvacuationDistance, BaseDoorPacker, BountarySegmentPacker_EvacuationDistance, ObstructurePacker_EvacuationDistance>.GetRoomPacker(document, list, list2, new List<BuiltInCategory>(), null, null, null, null);
			List<List<Curve>> list3 = null;
			List<Curve> list4 = new List<Curve>();
			Transaction transaction = new Transaction(document, "test");
			transaction.Start();
			foreach (BaseRoomPacker baseRoomPacker in roomPacker)
			{
				List<Curve> list5 = new List<Curve>();
				List<Curve> list6 = new List<Curve>();
				foreach (List<BaseBoundarySegmentPacker> list7 in baseRoomPacker.LstLstBoundarySegmentPacker)
				{
					foreach (BaseBoundarySegmentPacker baseBoundarySegmentPacker in list7)
					{
						Curve curve;
						list3 = Utility.DoBoundarySgementSplite(baseBoundarySegmentPacker, out curve, true);
						foreach (Curve curve2 in list3[1])
						{
							list4.Add(curve2);
							list5.Add(Utility.GetAOffestCurveInRoom(curve2, baseBoundarySegmentPacker.ThisRoomPacker.ThisRoom, 3.0));
							document.Create.NewDetailCurve(activeUIDocument.ActiveView, curve2);
						}
						foreach (Curve curve3 in list3[0])
						{
							document.Create.NewDetailCurve(activeUIDocument.ActiveView, curve3);
							document.Create.NewDetailCurve(activeUIDocument.ActiveView, curve3);
						}
					}
					list5 = Utility.GetCutedLstCurves(list5);
					foreach (Curve curve4 in list5)
					{
						document.Create.NewDetailCurve(activeUIDocument.ActiveView, curve4);
					}
					for (int i = 0; i < list4.Count; i++)
					{
						list6.Add(Line.CreateBound(list5[i].GetEndPoint(0), list4[i].GetEndPoint(0)));
						list6.Add(Line.CreateBound(list5[i].GetEndPoint(1), list4[i].GetEndPoint(1)));
					}
					foreach (Curve curve5 in list6)
					{
						document.Create.NewDetailCurve(activeUIDocument.ActiveView, curve5);
					}
				}
			}
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
